package com.mingtu.jgspatrol.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.StringUtils;
import com.mingtu.jgspatrol.R;
import com.mingtu.jgspatrol.bean.GradeBean;
import com.mingtu.jgspatrol.databinding.ItemTrackResultRankingBinding;
import com.mingtu.jgspatrol.holder.BaseViewBindingHolder;

import java.util.List;


/**
 * 描述：
 * 修改人：
 * 修改时间：
 * 修改备注：
 *
 * @author wkk
 */

public class ResultRankingAdapter extends RecyclerView.Adapter<BaseViewBindingHolder> {

    private Context context;
    private List<GradeBean.PageBean.ListBean> mData;


    public ResultRankingAdapter(Context context, List<GradeBean.PageBean.ListBean> mData) {
        this.context = context;
        this.mData = mData;
    }

    @Override
    public BaseViewBindingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
//        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_track_result_ranking, parent, false);
        ItemTrackResultRankingBinding binding = ItemTrackResultRankingBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        // 实例化viewholder
        BaseViewBindingHolder viewHolder = new BaseViewBindingHolder(binding);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(BaseViewBindingHolder holder, int position) {

        if (mData != null) {
            ItemTrackResultRankingBinding binding = (ItemTrackResultRankingBinding) holder.getBinding();
            GradeBean.PageBean.ListBean listBean = mData.get(position);
            String name = listBean.getName().toString().trim();
            String mileage = listBean.getTotalMileage();
            int taskNum = listBean.getTaskNum();
            int clockNum = listBean.getClockNum();

            binding.tvNum.setText((position + 1) + "");


            if (position == 0) {
                binding.tvNum.setBackground(context.getDrawable(R.drawable.shape_circle_green));
            } else if (position == 1) {
                binding.tvNum.setBackground(context.getDrawable(R.drawable.shape_circle_ffc));
            } else if (position == 2) {
                binding.tvNum.setBackground(context.getDrawable(R.drawable.shape_circle_red));
            } else {
                binding.tvNum.setBackgroundColor(context.getResources().getColor(R.color.transparent));
                binding.tvNum.setTextColor(context.getResources().getColor(R.color.text_color333));
            }


            if (!StringUtils.isEmpty(name)) {
                binding.tvName.setText(name);
            }

            if (!StringUtils.isEmpty(mileage)) {
                binding.tvMileage.setText(mileage + "km");
            }

            binding.tvDay.setText(taskNum + "天");
            binding.tvSignInNum.setText(clockNum + "次");
        }
        //将position保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public void upData(List<GradeBean.PageBean.ListBean> data) {
        mData = data;
        notifyDataSetChanged();
    }

}
